package com.ruiya.staging.goods.dataio.dao;

import com.ruiya.staging.data.core.db.SqlExecutor;
import com.ruiya.staging.goods.dataio.cfg.GoodsDbConfig;
import com.ruiya.staging.goods.dataio.model.GoodsInfoDO;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class GoodsInfoDao {
    private static final Logger _log = LoggerFactory.getLogger(GoodsInfoDao.class);

    @Autowired
    private GoodsDbConfig goodsDbConfig;

    public GoodsInfoDO getGoodsInfo(String sharding, String goodsId) {
        _log.info("goodsDbConfig.url={}", goodsDbConfig.getUrl());
        SqlExecutor.SqlSession session = SqlExecutor.openSession(goodsDbConfig.getUrl());
        try {
            return session.select("SELECT * FROM goods_info WHERE goods_id=?", rs -> {
                GoodsInfoDO item = null;
                if (rs.next()) {
                    item = new GoodsInfoDO();
                    item.setEntId(rs.getString("ent_id"));
                    item.setGoodsId(rs.getString("goods_id"));
                    item.setGoodsNo(rs.getString("goods_no"));
                    item.setGoodsName(rs.getString("goods_name"));
                    item.setApprovalNo(StringUtils.isEmpty(rs.getString("approval_no")) ? null : rs.getString("approval_no"));
                    item.setBarcode(StringUtils.isEmpty(rs.getString("barcode")) ? null : rs.getString("barcode"));
                    item.setGoodsHabitat(StringUtils.isEmpty(rs.getString("goods_habitat")) ? null : rs.getString("goods_habitat"));
                    item.setGoodsPicture(StringUtils.isEmpty(rs.getString("goods_picture")) ? null : rs.getString("goods_picture"));
                    item.setGoodsSpecs(StringUtils.isEmpty(rs.getString("goods_specs")) ? null : rs.getString("goods_specs"));
                    item.setDosage(StringUtils.isEmpty(rs.getString("dosage")) ? null : rs.getString("dosage"));
                    item.setGoodsTag(StringUtils.isEmpty(rs.getString("goods_tag")) ? null : rs.getString("goods_tag"));
                    item.setGoodsUnit(StringUtils.isEmpty(rs.getString("goods_unit")) ? null : rs.getString("goods_unit"));
                    item.setManufacturer(StringUtils.isEmpty(rs.getString("manufacturer")) ? null : rs.getString("manufacturer"));
                    item.setNote(StringUtils.isEmpty(rs.getString("note")) ? null : rs.getString("note"));
                    item.setPinyinInitial(StringUtils.isEmpty(rs.getString("pinyin_initial")) ? null : rs.getString("pinyin_initial"));
                    item.setPlatformCode(StringUtils.isEmpty(rs.getString("platform_code")) ? null : rs.getString("platform_code"));
                    item.setStatus(StringUtils.isEmpty(rs.getString("status")) ? null : rs.getString("status"));
                    item.setCreateTime(rs.getDate("create_time"));
                    item.setCreator(StringUtils.isEmpty(rs.getString("creator")) ? null : rs.getString("creator"));
                    item.setLastModifiedTime(rs.getDate("lastModifiedTime"));
                    item.setLastModifier(StringUtils.isEmpty(rs.getString("last_modifier")) ? null : rs.getString("last_modifier"));
                }
                return item;
            }, goodsId);
        } finally {
            session.close();
        }
    }

    public Pair<Boolean, String> createGoodsInfo(String sharding, GoodsInfoDO goodsInfoDTO){
        _log.info("goodsDbConfig.url={}", goodsDbConfig.getUrl());
        SqlExecutor.SqlSession session = SqlExecutor.openSession(goodsDbConfig.getUrl());
        try {
            //TODO: unfinished
            int ret = session.insert("insert into goods_info (goods_id,ent_id,goods_no,goods_name) values (?,?,?,?)",
                    goodsInfoDTO.getGoodsId(),
                            goodsInfoDTO.getEntId(),
                            goodsInfoDTO.getGoodsNo(),
                            goodsInfoDTO.getGoodsName());
            if(ret == 1){
                return new ImmutablePair<>(true, goodsInfoDTO.getGoodsId());
            }
            return new ImmutablePair<>(false, null);
        } finally {
            session.close();
        }
    }

    public String locateGoodsId(String goodsNo) {
        _log.info("goodsDbConfig.url={}", goodsDbConfig.getUrl());
        //suppose: have a goods global db, and have a map: goodsNo -> goodsId
        SqlExecutor.SqlSession session = SqlExecutor.openSession(goodsDbConfig.getUrl());
        try {
            String ret = session.select("SELECT goods_id FROM goods_info WHERE goods_no=?", rs -> {
                if (rs.next()) {
                    return rs.getString("goods_id");
                }
                return null;
            }, goodsNo);
            return ret;
        } finally {
            session.close();
        }
    }

}
